package com.hiss.basic.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hiss.basic.client.system.ISysLoginLogService;
import com.hiss.basic.model.system.domain.SysLoginLogDomain;
import com.hiss.basic.model.system.request.SysLoginLogQueryRequest;
import com.hiss.basic.model.system.response.SysLoginLogQueryResponse;
import com.hiss.basic.starter.mybatis.utils.PageUtils;
import com.hiss.basic.system.mapper.SysLoginLogMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 系统登录日志 service impl
 */
@Slf4j
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLogDomain> implements ISysLoginLogService {

    @Override
    public Page<SysLoginLogQueryResponse> findPageList(SysLoginLogQueryRequest request) {
        QueryWrapper wrapper = new QueryWrapper();
        if (ObjectUtil.isNotNull(request)) {
            wrapper.like(SysLoginLogDomain::getUsername, request.getUsername(), StrUtil.isNotBlank(request.getUsername()));
            wrapper.like(SysLoginLogDomain::getIp, request.getIp(), StrUtil.isNotBlank(request.getUsername()));
            wrapper.eq(SysLoginLogDomain::getStatus, request.getStatus(), ObjectUtil.isNotNull(request.getStatus()));
            wrapper.ge(SysLoginLogDomain::getLoginTime, request.getBeginTime(), ObjectUtil.isNotNull(request.getBeginTime()));
            wrapper.le(SysLoginLogDomain::getLoginTime, request.getEndTime(), ObjectUtil.isNotNull(request.getEndTime()));
        }
        wrapper.orderBy(SysLoginLogDomain::getCreateTime, false);

        Page page = PageUtils.getPage();
        return pageAs(page, wrapper, SysLoginLogQueryResponse.class);
    }
}
